package com.chen.apply.controller;


import com.chen.apply.utils.AliOssUtil;
import com.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * 通用接口
 */
@RestController
@RequestMapping("common")
@Slf4j
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file, @RequestParam String type){
        try {
            String originalFilename = file.getOriginalFilename();
            //获取后缀
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String objectName = UUID.randomUUID().toString()+suffix;

            //文件请求路径
            String filePath = aliOssUtil.upload(file.getBytes(), type+"/"+objectName);
            return  Result.success(filePath);
        } catch (IOException e) {
            log.error("Error uploading file");
        }
        return Result.error("上传文件失败");
    }
}















